FC = mpif90 

-include select_compiler.inc
#COMPILER = GNU/INTEL/PGI/IBM
#OPENMP = YES/NO
#GPU = YES/NO
#DEBUGGING = YES/NO
#POST_RUNTIME=YES/NO

ifeq ($(GPU),YES)
ifeq ($(OPENMP),YES)
  $(error GPU and OPENMP cannot be set at the same time!)
endif
endif

ifeq ($(GPU),YES)
  STORAGE_OBJ=storage_allocatable_acc.o
  STORAGE_SRC=storage_allocatable_acc.F90
else
  STORAGE_OBJ=storage_allocatable_omp.o
  STORAGE_SRC=storage_allocatable_omp.F90
endif

ifeq ($(POST_RUNTIME),YES)
   DFLAGS=-DPOST_RUNTIME
   DPOST_OBJ=post-init-eval.o
endif

ifeq ($(COMPILER),GNU)
  DEBUG = -fbounds-check -ffpe-trap=zero,overflow,invalid,underflow -g -fbacktrace
  ifeq ($(OPENMP),YES)
    OPT = -O3 -fopenmp -fno-range-check # the last needed by random_psdes
  else
    OPT = -O3 -fno-range-check # the last needed by random_psdes
  endif
endif
ifeq ($(COMPILER),INTEL)
  DEBUG = -fpe0 -traceback -C -g
  ifeq ($(OPENMP),YES)
    OPT = -O3 -ipo -xHost -openmp
  else
    OPT = -O3 -ipo -xHost
  endif
endif
ifeq ($(COMPILER),PGI)
  DEBUG = -Ktrap=fp -traceback -g
  ifeq ($(OPENMP),YES)
    OPT = -fast -Minfo -mp=nonuma -Dpgf95
  else ifeq ($(GPU),YES)
    OPT = -fast -ta=nvidia,cuda4.2,cc20,time -Minfo -Dpgf95
  else
    OPT = -fast -Minfo -Dpgf95
  endif
endif
ifeq ($(COMPILER),IBM)
  DEBUG = -C -qdbg=level=9 -qflttrap=invalid,nanq,overflow,zerodivide 
  ifeq ($(OPENMP),YES)
    FC = mpixlf90_r
    OPT = -O3 -qsmp=omp:noauto -qstrict -qipa=partition=large
  else
    FC = mpixlf90
    OPT = -O3 -qstrict -qipa=partition=large
  endif
endif

ifeq ($(DEBUGGING),YES)
  FFLAGS = $(DEBUG) $(DPOST_RUNTIME)
else
  FFLAGS = $(OPT) $(DPOST_RUNTIME)
endif

MAIN = cubeflu

OBJ = IR_Precision.o Lib_VTK_IO.o basic_var_acc.o $(STORAGE_OBJ) mpi_utils_new.o mpi_decompose.o mpi_update_new_omp.o mpi_update_new_acc.o update_acc.o allocate_storage.o mod_tools.o $(MAIN).o comp_init.o rk-init.o rk-eval_omp.o rk-eval_acc.o inpout_MPI.o bc_rhs_MPI_acc.o bc_rhs_k_MPI_acc.o bc_var_MPI_acc.o  bc_var_k_MPI_acc.o bc_rhs_MPI_omp.o bc_rhs_k_MPI_omp.o bc_var_MPI_omp.o bc_var_k_MPI_omp.o print_screen_MPI.o random.o scalars.o output_visual.o $(DPOST_OBJ) manual.o

%.o: %.f90 $(STORAGE_SRC)
	$(FC) $(FFLAGS) $(DFLAGS) -c $< 

%.o: %.F90 $(STORAGE_SRC)
	$(FC) $(FFLAGS) $(DFLAGS) -c $< 

main: $(OBJ) 
	echo "Compiling CUBE-Flu using $(COMPILER) compiler with OpenMP support = $(OPENMP) and GPU support = $(GPU)"
	$(FC) $(FFLAGS) $(DFLAGS) $(OBJ) -o $(MAIN) $(LIBS)

all:
	make clean ; make

clean:
	rm -f $(OBJ) *.mod $(MAIN)
